package com.caimiao.miao.page.master.system.bluetooth;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.os.Build;

import com.caimiao.miao.interfaces.BasePresenterImpl;
import com.caimiao.miao.library.bluetooth.BTDevice;
import com.caimiao.miao.library.utils.CRC16Util;
import com.caimiao.miao.http.rxjava.transformer.SchedulerTransformer;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class BluetoothPresenter extends BasePresenterImpl<IBluetoothContact.View, IBluetoothContact.Model> implements IBluetoothContact.Presenter {
    private BluetoothAdapter mAdapter;

    public BluetoothPresenter(IBluetoothContact.View mView) {
        super(mView);
    }

    @Override
    protected IBluetoothContact.Model getModel() {
        return new BluetoothModel();
    }

    @Override
    public void start(Context context) {
        boolean enable = mModel.initBluetooth(context);
        if (enable) {
            findBluetoothDevice(context);
        }
    }

    @Override
    public void startDiscovery(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            addDisposable(new RxPermissions(activity)
                    .request(Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION)
                    .compose(SchedulerTransformer.applyIoAndUi())
                    .subscribe(new Consumer<Boolean>() {
                        @Override
                        public void accept(Boolean aBoolean) throws Exception {
                            if (aBoolean == true) mModel.startDiscovery();
                        }
                    }));
        } else {
            mModel.startDiscovery();
        }
    }

    @Override
    public void findBluetoothDevice(Context context) {
        mAdapter = new BluetoothAdapter(context, mModel.getBondedDevices());
        mView.setAdapter(mAdapter);

        Disposable disposable = mModel.getDevices()
                .compose(SchedulerTransformer.applyIoAndUi())
                .subscribe(new Consumer<BTDevice>() {
                    @Override
                    public void accept(BTDevice device) throws Exception {
                        mAdapter.add(device);
                    }
                });
        addDisposable(disposable);
    }

    @Override
    public void connect(BTDevice device) {
        mModel.connect(device);
        Disposable disposable = mModel.read()
                .compose(SchedulerTransformer.applyIoAndUi())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        mView.setRead(s);
                    }
                });
        addDisposable(disposable);
    }

    @Override
    public void send(String value) {
        byte[] sbuf = CRC16Util.getSendBuf(value.replace(" ", ""));
//        char[] send = {0xff, 0x55, 0xd0, 0x01, 0x00};
//        sbuf = CRC16Util.getSendBuf(send);
        mView.setRead(CRC16Util.getBufHexStr(sbuf));

        /*FF 55 A2 01 64 23 C0
          FF 55 F0 01 00 26 F0
          FF 55 A0 01 00 22 B0
          FF 55 A1 01 00 A2 A7
          FF 55 B0 01 00 A3 F3
          FF 55 D0 01 00 A4 73*/

//        byte[] bytes = DecimalUtil.hexStr2Bytes(value);
//        mModel.write(bytes);
    }

    @Override
    public void detach() {
        super.detach();
        mModel.unregisterBluetoothReceiver();
    }

}
